package com.miaoquan.mqapitbk.task;

import com.miaoquan.mqapitbk.domain.builder.TbkProductBuilder;
import com.miaoquan.mqapitbk.utils.TbkUtil;
import com.miaoquan.mqmbg.mapper.TbkProductMapper;
import com.miaoquan.mqmbg.model.TbkProduct;
import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkDgOptimusMaterialRequest;
import com.taobao.api.response.TbkDgOptimusMaterialResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 订单超时取消并解锁库存的定时器
 * cron表达式：Seconds Minutes Hours DayofMonth Month DayofWeek [Year]
 * <p>
 * Created by macro on 2018/8/24.
 */
@Slf4j
@Component
public class TbkProductTask {

    @Autowired
    private TaobaoClient taobaoClient;
    @Autowired
    private TbkProductMapper tbkProductMapper;

    /**
     * 淘宝客-推广者-物料精选
     * 每1分钟执行一次
     */
    @Scheduled(cron = "0 0/1 * ? * ?")
    private void optimusMaterial() throws ApiException {
        TbkDgOptimusMaterialRequest req = new TbkDgOptimusMaterialRequest();
        req.setAdzoneId(TbkUtil.ADZONE_ID);
        req.setPageSize(20L);
        req.setPageNo(1L);
        req.setMaterialId(6708L);
//        req.setDeviceValue("xxx");
//        req.setDeviceEncrypt("MD5");
//        req.setDeviceType("IMEI");
//        req.setContentId(323L);
//        req.setContentSource("xxx");
//        req.setItemId(33243L);
//        req.setFavoritesId("123445");
        TbkDgOptimusMaterialResponse rsp = taobaoClient.execute(req);
        if (rsp.isSuccess()) {
            List<TbkDgOptimusMaterialResponse.MapData> resultList = rsp.getResultList();
            resultList.forEach(mapData -> {
                TbkProduct tbkProduct = new TbkProductBuilder().buildByOptimusMaterialMapData(mapData);
                tbkProductMapper.insertSelective(tbkProduct);
            });
        }
//        System.out.println(rsp.getBody());
    }
}
